'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const {
    router,
    controller: {
      clients,
      home,
      oidc,
    } } = app;

  // 通过egg-zconnect-plugin插件的genCallback生成固定的鉴权callback
  const callbackController = app.genCallback('zeehub', ctx => {
    const userInfo = ctx.getUserInfo('zeehub');
    const isManager = ctx.app.config.manager.includes(userInfo.sub);
    ctx.session.zeehub = Object.assign(userInfo, { isManager });
    ctx.redirect('/');
  });
  router
    .get('/', home.index)
    .get('/logout', home.logout)
    .get('/callback', callbackController)
    .get('/background', home.managerHtml)
    .get('/docs/', home.docs);
  router
    .get('/apply_client', clients.applyHtml)
    .post('/userapi/clients', clients.applyClient);

  router
    .get('/managerapi/clients', clients.index)
    .post('/managerapi/clients', clients.create)
    .delete('/managerapi/clients/:id', clients.delete)
    .put('/managerapi/clients/:id', clients.update)
    .get('/managerapi/test', clients.test);
  router
    .get('/authorize', oidc.authorize)
    .post('/oidc/token', oidc.token)  // 获取token
    // .post('/oidc/authenticate', oidc.authenticate)    // 验证请求
    .post('/oidc/register', oidc.register)
    .post('/oidc/check_psw', oidc.checkPsw)
    .get('/oidc/logout', oidc.logout);
};
